﻿/**
 * @usage	单一加载容器，可链接到库(基类:org.nickro.project.load.SimpleLoader)
 * <code>
 * 		var loader:SimpleLoader = new SimpleLoader();
 * 		loader.loadSwf('hello.swf');
 * 		loader.progress = onProgress;
 * 		loader.result = onResult;
 * 		loader.fault = onFault;
 * 		function onProgress(p:int):void
 * 		{
 * 			//p加载进程
 * 		}
 * 		function onResult(e:ResultEvent):void
 * 		{
 * 			//e.data返回的内容
 * 		}
 * 		function onFault(e:FaultEvent):void
 * 		{
 * 			//e.message错误信息
 * 		}
 * </code>
 * 
 * 
 */
package org.nickro.project.load 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.IEventDispatcher;
	import flash.errors.IOError;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import org.nickro.project.events.ResultEvent;
	import org.nickro.project.events.FaultEvent;
	/**
	 * ...
	 * @author com.nickro
	 */
	public class SimpleLoader extends Sprite
	{
		
		public function SimpleLoader() 
		{
			addEventListener(ResultEvent.RESULT, onResult);
			addEventListener(FaultEvent.FAULT, onFault);
		}

		private var loader:Loader;
		private var _url:String;
		public function loadSwf(url:String):void
		{
			_url = url;
			loader = new Loader();
			start();
		}
		
		/**
		 *	停止加载操作
		 */
		public function stop():void
		{
			deleteEventListener(loader.contentLoaderInfo);
			loader.close();
		}
		
		/**
		 * 恢复加载操作
		 */
		public function start():void
		{
			loader.load(new URLRequest(_url));
			configEventListener(loader.contentLoaderInfo);
		}
		
		public function die():void
		{
			deleteEventListener(loader.contentLoaderInfo);
		}
		
		private function configEventListener(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(ProgressEvent.PROGRESS,onProgress);
			dispatcher.addEventListener(Event.COMPLETE,onComplete);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecError);
		}
		
		private function deleteEventListener(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(ProgressEvent.PROGRESS,onProgress);
			dispatcher.removeEventListener(Event.COMPLETE,onComplete);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
			dispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecError);
		}
		
		private function onProgress(e:ProgressEvent):void
		{
			if (progress != null) {
				var percent:int = int((e.bytesLoaded / e.bytesTotal) * 100);
				progress.apply(this, [percent]);
			}
		}
		private function onComplete(e:Event):void
		{
			dispatchEvent(new ResultEvent(ResultEvent.RESULT, e.target.content));
		}
		
		private function onIOError(e:IOErrorEvent):void
		{
			throw new IOError('"' + _url + '"' + " is not existed");
			dispatchEvent(new FaultEvent(FaultEvent.FAULT, e.text));
		}
		
		private function onSecError(e:SecurityErrorEvent):void
		{
			throw new SecurityError(this + "安全问题");
			dispatchEvent(new FaultEvent(FaultEvent.FAULT, e.text));
		}
		
		private function onResult(e:ResultEvent):void
		{
			if (result != null) result.apply(null, [e]);
		}
		
		private function onFault(e:FaultEvent):void
		{
			if (fault != null) fault.apply(null, [e]);
		}
		
		//过程处理
		private var _progress:Function = null;
		public function get progress():Function { return _progress; }
		public function set progress(value:Function):void { _progress = value; }
		//返回处理
		private var _result:Function = null;
		public function set result(value:Function):void { _result = value; };
		public function get result():Function { return _result; };
		//错误处理
		private var _fault:Function = null;
		public function get fault():Function { return _fault; };
		public function set fault(value:Function):void { _fault = value; };
	}

}